﻿<!DOCTYPE html>
<html>
 <head></head> 
 <body> 
  <div id="show" style="height:100px;width:200px;"> 
  </div> 
  <div style="height:100px;width:520px;"> 
   <div id="operation1" style="height:100px;width:130px;display:inline-block;"> 
   </div> 
   <div style="height:100px; width:10px;display:inline-block;">
     = 
   </div> 
   <div style = "height:100px;width:100px;display:inline-block;">
    <input type = "text" id = "answer1" style = "width:50px;" /> 
   </div>
   <div id="result1" style="height:100px;width:100px;display:inline-block;"> 
   </div>
   <div style = "height:100px;width:100px;display:inline-block;">
    <button onclick = "check(1)">点</button>
    <span id ="check1">返回结果为0，则正确，1,2,3,4,5分别代表错误类型，加，减，乘，除，多项。<br /></span>
   </div> 
  </div>  
 </body>
</html>
<script language = "javascript">
//save results.
var result = new Array();
//save operates
var operates = new Array(" + ", " - ", " &times; ", " &divide; ");
var answer_type = new Array();
//easy mode or hard mode or other modes
mode = 1;
//only one problem to test
createStr(1);
calculateAnswer(1);

//Main function: need use suitable 'id' to create random problem.
function createStr(id) {
    var obj = document.getElementById("operation" + id);
    var value;
    if (mode == 0) value = createEasy(id);
    else if(mode == 1) value = createHard(id);
    else if(mode == 2) value = createAddition(id);
    else if(mode == 3) value = createSubtraction(id);
    else if(mode == 4) value = createTimes(id);
    else if(mode == 5) value = createDivide(id);
    obj.innerHTML = value;
}
//Another main function: need use suitable 'id' to get corresponding answer.
function calculateAnswer(id) {
    document.getElementById("result" + id).innerHTML = result[id];
    //another way to solve the problem
    /*var obj = document.getElementById("operation" + id);
    var str = obj.innerHTML;
    var value = 0;
    //43 +, 45 -, 215 *, 247 /
    var operate = str.charCodeAt(str.indexOf(" ") + 1);
    if(operate == 43)
        value = calculate(0, str.substring(0, str.indexOf("+") - 1), str.substring(str.indexOf("+") + 2));
    else if(operate == 45)
        value = calculate(1, str.substring(0, str.indexOf("-") - 1), str.substring(str.indexOf("-") + 2));
    else if(operate == 215)
        value = calculate(2, str.substring(0, str.indexOf(" ")), str.substring(str.indexOf(" ") + 3));
    else if(operate == 247)
        value = calculate(3, str.substring(0, str.indexOf(" ")), str.substring(str.indexOf(" ") + 3));
    document.getElementById("result" + id).innerHTML = "result: " + value + "<br />测试result[" + id + "]: " + result[id];*/
    // alert(str);
    // if(str == " &times; ")
    //  document.write(1);
    // else
    //  document.write(2);
}
function check(id){
    var content;
    if(document.getElementById("answer" + id).value == result[id])
        content = 0;
    else
        content = answer_type[id];
    document.getElementById("check" + id).innerHTML = content;
}
//no need to read more!!!!!!!!!!!!!!!!!!!!!!!!! 

//return Operation String
function createEasy(id) {
    var num = randomNumber(4);
    var type = Math.floor(num);
    var operate = operates[type]; // getOperates
    var number_one = Math.floor(randomNumber(100));
    var number_two = Math.floor(randomNumber(100));
    if (type == 3){
    	if(number_two == 0)
    		number_two = 1;
    	if(number_two > 20)
    		number_two = Math.floor(number_two / 2);
    	number_one = (Math.round(number_one / number_two) + 1) * number_two;
        answer_type[id] = 4;
    }else if(type == 1 && number_one < number_two){
        number_tmp = number_one;
        number_one = number_two;
        number_two = number_tmp;
        answer_type[id] = 2;
    } else if (type == 2 && number_two > 10) {
        number_two = Math.round(number_two / 10);
        answer_type[id] = 3;
    }else{
        answer_type[id] = 1;
    }
    result[id] = calculate(type, number_one, number_two);
    return number_one + operate + number_two;
}

function createAddition(id){
    var number_one = Math.floor(randomNumber(100));
    var number_two = Math.floor(randomNumber(100));
    var operate = operates[0];
    result[id] = calculate(type, number_one, number_two);
    return number_one + operate + number_two;
}
function createSubtraction(id){
    var number_one = Math.floor(randomNumber(100));
    var number_two = Math.floor(randomNumber(100));
    var operate = operates[1];
    if(number_one < number_two){
        number_tmp = number_one;
        number_one = number_two;
        number_two = number_tmp;
    }
    result[id] = calculate(type, number_one, number_two);
    return number_one + operate + number_two;
}
function createTimes(id){
    var number_one = Math.floor(randomNumber(100));
    var number_two = Math.floor(randomNumber(100));
    var operate = operates[2];
    if (number_two > 10) number_two = Math.round(number_two / 10);
    result[id] = calculate(type, number_one, number_two);
    return number_one + operate + number_two;
}
function createDivide(id){
    var number_one = Math.floor(randomNumber(100));
    var number_two = Math.floor(randomNumber(100));
    var operate = operates[2];
    if(number_two == 0)
        number_two = 1;
    if(number_two > 20)
        number_two = Math.floor(number_two / 2);
    number_one = (Math.round(number_one / number_two) + 1) * number_two;
    result[id] = calculate(type, number_one, number_two);
    return number_one + operate + number_two;
}
//return Operation String
function createHard(id) {
    answer_type[id] = 5;
    var count_half;
    var type_one = randomNumber(4);
    var type_two = randomNumber(4);
    var operate_one = operates[type_one];
    var operate_two = operates[type_two];
    var number_one = randomNumber(100);
    var number_two = randomNumber(100);
    var number_three = randomNumber(100);
    //next two lines for test!
    document.getElementById("show").innerHTML = "number_one: " + number_one + "<br/>number_two: " + number_two + "<br/>number_three: " + number_three + "<br/>type_one: " + operate_one + "<br/>type_two: " + operate_two;
    //whether add parentheses but 50% hava the parentheses
    if(((type_one < 2 && type_two > 1) || (type_one > 1 && type_two < 2)) && randomNumber(2) == 0){
    	if(type_one < 2){
    		if(type_one == 1 && number_one < number_two){
                tmp = number_one;
                number_one = number_two;
                number_two = number_one;
            }
            count_half = calculate(type_one, number_one, number_two);
            if(type_two == 3){
                if(number_three == 0)
                    number_three = 1;
                if(number_three > 20)
                    number_three = Math.floor(number_three / 2);
                num = (Math.round(count_half / number_three) + 1) * number_three - count_half;
                number_one += num;
                count_half += num;
            }
            result[id] = calculate(type_two, count_half, number_three);
            return "(" + number_one + operate_one + number_two + ")" + operate_two + number_three;
    	}else{
            if(type_two == 1 && number_two < number_three){
                tmp = number_two;
                number_two = number_three;
                number_three = tmp;
            }
            count_half = calculate(type_two, number_two, number_three);
            if(type_one == 3){
                if(count_half == 0){
                    count_half += 1;
                    number_two += 1;
                }
                if(count_half > 20){
                    tmp = count_half - Math.floor(count_half / 2);
                    number_two -= tmp;
                    //for fear that the number_two maybe minus number
                    if(number_two < 0){
                        number_three -= 10 - number_two;
                        number_two = 10;
                    }
                    count_half -= tmp;
                }
                number_one = (Math.round(number_one / count_half) + 1) * count_half;
            }
            result[id] = calculate(type_one, number_one, count_half);
            return number_one + operate_one + "(" + number_two + operate_two + number_three +")";
        }
    }
    //no parentheses added
    else{
        // all times or divide
        if(type_one * type_two > 3){
            if(type_one == 3){
                if(number_two == 0)
                    number_two = 1;
                if(number_two > 20)
                    number_two = Math.floor(number_two / 2);
                number_one = (Math.round(number_one / number_two) + 1) * number_two;
            }
            count_half = calculate(type_one, number_one, number_two);
            if(type_two == 3){
                if(number_three == 0)
                    number_three = 1;
                if(number_three > 20)
                    number_three = Math.floor(number_three / 2);
                number_one = Math.floor(number_three / lcm(number_three, count_half)) * number_one;
                count_half = Math.floor(number_three / lcm(number_three, count_half)) * count_half;
            }
            if(type_one == 2 && type_two == 2){
                number_two = Math.floor(number_two / 10) + 1;
                number_three = Math.floor(number_three / 10) + 1;
            }
            result[id] = calculate(type_two, count_half, number_three);
            return number_one + operate_one + number_two + operate_two + number_three;
        }
        else if(type_one < 2 && type_two < 2){
            if(type_one == 1 && number_one < number_two){
                tmp = number_one;
                number_one = number_two;
                number_two = tmp;
            }
            count_half = calculate(type_one, number_one, number_two);
            if(type_two == 1 && number_three > count_half){
                result[id] = number_three - count_half;
                return number_three + operate_two + "(" + number_one + operate_one + number_two + ")";
            }
            result[id] = calculate(type_two, count_half, number_three);
            return number_one + operate_one + number_two + operate_two + number_three;
        }else{
            if(type_one < 2){
                if(type_two == 3){
                    if(number_three == 0)
                        number_three = 1;
                    if(number_three > 20)
                        number_three = Math.floor(number_three / 2);
                    number_two = (Math.floor(number_two / number_three) + 1) * number_three;
                }
                count_half = calculate(type_two, number_two, number_three);
                if(type_one == 1 && number_one < count_half){
                    result[id] = count_half - number_one;
                    return number_two + operate_two + number_three + operate_one + number_one;
                }
                result[id] = calculate(type_one, number_one, count_half);
                return number_one + operate_one + number_two + operate_two + number_three; 
            }else{
                if(type_one == 3){
                    if(number_two == 0)
                        number_two = 1;
                    if(number_two > 20)
                        number_two = Math.floor(number_two / 2);
                    number_one = (Math.floor(number_one / number_two) + 1) * number_two;
                }
                count_half = calculate(type_one, number_one, number_two);
                if(type_two == 1 && count_half < number_three){
                    result[id] = number_three - count_half;
                    return number_three + operate_two + number_one + operate_one + number_two;
                }
                result[id] = calculate(type_two, count_half, number_three);
                return number_one + operate_one + number_two + operate_two + number_three;
            }
        }
    }
}

function randomNumber(max) {
    return Math.floor(Math.random() * max);
}

function calculate(type, num_one, num_two) {
    if (type == 0) return parseInt(num_one) + parseInt(num_two);
    else if (type == 1) return parseInt(num_one) - parseInt(num_two);
    else if (type == 2) return Math.round(parseInt(num_one) * parseInt(num_two));
    else return Math.round(parseInt(num_one) / parseInt(num_two));
}

function lcm(m,n){//辗转相除法 求最大公约数
    var u=m,v=n,t=v;
    while(v!=0){
        t=u%v;
        u=v;
        v=t;
    }
    return u;
}

</script>
